package gemini.castor.ui.client;

import gemini.castor.ui.client.mvp.PlaceRequestManager;
import gemini.castor.ui.client.mvp.gin.CastorGinjector;
import gemini.castor.ui.client.page.RootPresenter;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.RootPanel;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class CastorApp implements EntryPoint {

    private static CastorApp instance;

    private CastorApp() {
        instance = this;
    }

    public static CastorApp getInstance() {
        if (instance == null) {
            instance = new CastorApp();
        }
        return instance;
    }



    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        /*
         * Use a deferred command so that the UncaughtExceptionHandler catches
         * any exceptions in bootStrap()
         */
        DeferredCommand.addCommand(new Command() {
            public void execute() {
                startUp();
            }
        });
    }

    /**
     * This is the entry point method for this application
     */
    public void startUp() {
    	initApp();
    }

    private void initApp() {
        final CastorGinjector ginjector = CastorGinjector.INSTANCE;
        PlaceRequestManager prm = ginjector.getPlaceRequestManager();
        prm.registerHandlers();

        final RootPresenter rootPresenter = ginjector.getRootPresenter();

        rootPresenter.bind();

        RootPanel.get("content").add(rootPresenter.getDisplay().asWidget());      
    }

}
